package com.rybbaby.tss.core.utils;

import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author 谢进伟
 * @description Http 请求工具类
 * @createDate 2016年9月20日 下午5:26:47
 */
public class HttpUtil {

    private static final String ENCODING = "UTF-8";
    private static Logger log = Logger.getLogger(HttpUtil.class);

    /**
     * 发起远程Http请求
     *
     * @param httpUrl   请求地址
     * @param paramsStr 参数字符串 格式：paramNmae1=paramValue1 & paramName2=paramValue2...
     * @return
     * @throws Exception
     */
    public static String sendHttpsRemoteRequest(String httpUrl, RequestMethod method, String paramsStr) {
        return sendRemoteRequest(httpUrl, true, method, paramsStr);
    }

    /**
     * 发起远程Http请求
     *
     * @param httpUrl   请求地址
     * @param paramsStr 参数字符串 格式：paramNmae1=paramValue1 & paramName2=paramValue2...
     * @return
     * @throws Exception
     */
    public static String sendHttpRemoteRequest(String httpUrl, RequestMethod method, String paramsStr) {
        return sendRemoteRequest(httpUrl, false, method, paramsStr);
    }

    /**
     * 发起远程 Https 形式的get请求请求
     *
     * @param url    请求地址
     * @param params 参数
     * @return
     */
    public static String httpsGet(String url, Map<String, Object> params) {
        return sendHttpsRemoteRequest(url, RequestMethod.GET, switchParamsModel(params));
    }

    /**
     * 发起远程 Https 形式的pst请求请求
     *
     * @param url    请求地址
     * @param params 参数
     * @return
     */
    public static String httpsPost(String url, Map<String, Object> params) {
        return sendHttpsRemoteRequest(url, RequestMethod.POST, switchParamsModel(params));
    }

    /**
     * 发送 http 形式的get请求
     *
     * @param url        请求路径
     * @param returnType 请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpGet(String url, ReturnType returnType) throws HttpException, IOException {
        return httpGet(url, null, null, returnType);
    }

    /**
     * 发送 http 形式的get请求
     *
     * @param url        请求路径
     * @param params     参数
     * @param returnType 请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpGet(String url, Map<String, Object> params, ReturnType returnType) throws HttpException, IOException {
        return httpGet(url, null, params, returnType);
    }

    /**
     * 发送 http 形式的get请求
     *
     * @param url        请求路径
     * @param headers    http 头参数
     * @param params     参数
     * @param returnType 请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpGet(String url, Map<String, String> headers, Map<String, Object> params, ReturnType returnType) throws HttpException, IOException {
        GetMethod method = new GetMethod(url);
        if (headers != null && !headers.isEmpty()) {
            headers.keySet().parallelStream().forEach((String key) -> {
                method.addRequestHeader(key, headers.get(key));
            });
        }
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, StandardCharsets.UTF_8.toString());
        if (params != null && !params.isEmpty()) {// 设置参数
            NameValuePair[] NameValuePairs = new NameValuePair[params.size()];
            int index = 0;
            Set<String> keys = params.keySet();
            for (String parameterName : keys) {
                Object parameterValue = params.get(parameterName);
                parameterValue = parameterValue == null ? "" : parameterValue;
                NameValuePairs[index++] = new NameValuePair(parameterName, parameterValue.toString());
            }
            method.setQueryString(NameValuePairs);
        }
        HttpClient hc = new HttpClient();
        hc.executeMethod(method);
        if (returnType == null || (returnType != null && returnType == ReturnType.String)) {
            return method.getResponseBodyAsString();
        } else if (returnType == ReturnType.byteArray) {
            return method.getResponseBody();
        } else {
            return method.getResponseBodyAsStream();
        }
    }

    /**
     * 发送 http 形式的post请求
     *
     * @param url        请求路径
     * @param headers    http 头参数
     * @param params     参数
     * @param returnType 请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, headers, params, "", null, returnType);
    }

    /**
     * 发送 http 形式的post请求
     *
     * @param url        请求路径
     * @param params     参数
     * @param returnType 请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, Object> params, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, null, params, "", null, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url                 请求地址
     * @param params              post请求参数
     * @param requestBody         请求体
     * @param requestBodyMimeType 请求体MimeType,如：MimeType.APPLICATION_JSON
     * @param returnType          请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, Object> params, String requestBody, String requestBodyMimeType, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, null, params, requestBody, requestBodyMimeType, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url         请求地址
     * @param requestBody 请求体,请求体为JSON格式
     * @param returnType  请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, String requestBody, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, null, null, requestBody, MimeType.APPLICATION_JSON, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url                 请求地址
     * @param requestBody         请求体
     * @param requestBodyMimeType 请求体MimeType,如：MimeType.APPLICATION_JSON
     * @param returnType          请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, String requestBody, String requestBodyMimeType, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, null, null, requestBody, requestBodyMimeType, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url         请求地址
     * @param params      post请求参数
     * @param requestBody 请求体,请求体为JSON格式
     * @param returnType  请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, Object> params, String requestBody, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, null, params, requestBody, MimeType.APPLICATION_JSON, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url                 请求地址
     * @param headers             http 头参数
     * @param params              post请求参数
     * @param requestBody         请求体
     * @param requestBodyMimeType 请求体MimeType,如：MimeType.APPLICATION_JSON
     * @param returnType          请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, String requestBody, String requestBodyMimeType, ReturnType returnType) throws HttpException, IOException {
        RequestEntity requestEntilty = null;
        if (StringUtils.isNotBlank(requestBody)) {
            requestEntilty = new StringRequestEntity(requestBody, requestBodyMimeType, StandardCharsets.UTF_8.toString());
        }
        return httpPost(url, headers, params, requestEntilty, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url         请求地址
     * @param headers     http 头参数
     * @param params      post请求参数
     * @param requestBody 请求体，请求体为 JSON 格式
     * @param returnType  请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, String requestBody, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, headers, params, requestBody, MimeType.APPLICATION_JSON, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url                 请求地址
     * @param headers             http 头参数
     * @param params              post请求参数
     * @param inputStream         请求体(流的形式)
     * @param requestBodyMimeType 请求体MimeType,如：MimeType.IMAGE_PNG
     * @param returnType          请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, InputStream inputStream, String requestBodyMimeType, ReturnType returnType) throws HttpException, IOException {
        RequestEntity requestEntilty = null;
        if (inputStream != null) {
            requestEntilty = new InputStreamRequestEntity(inputStream, requestBodyMimeType);
        }
        return httpPost(url, headers, params, requestEntilty, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url         请求地址
     * @param headers     http 头参数
     * @param params      post请求参数
     * @param inputStream 请求体(流的形式)
     * @param returnType  请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, InputStream inputStream, ReturnType returnType) throws HttpException, IOException {
        return httpPost(url, headers, params, inputStream, null, returnType);
    }

    /**
     * 发送http post 请求
     *
     * @param url           请求地址
     * @param headers       http 头参数
     * @param params        post请求参数
     * @param requestEntity 请求体
     * @param returnType    请求结果返回形式
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static Object httpPost(String url, Map<String, String> headers, Map<String, Object> params, RequestEntity requestEntity, ReturnType returnType) throws HttpException, IOException {
        boolean isUseRequestBody = false;
        if (requestEntity != null) {
            String contentType = requestEntity.getContentType();
            isUseRequestBody = contentType.toLowerCase().contains(MimeType.APPLICATION_JSON);
        }
        List<String> urlParams = null;
        PostMethod method = new PostMethod(url);
        String charset = StandardCharsets.UTF_8.toString();
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
        if (params != null && !params.isEmpty()) {
            Set<String> keys = params.keySet();
            for (String parameterName : keys) {
                Object parameterValue = params.get(parameterName);
                parameterValue = parameterValue == null ? "" : parameterValue;
                if (isUseRequestBody) {
                    if (urlParams == null) {
                        urlParams = new ArrayList<>();
                    }
                    urlParams.add(parameterName + "=" + parameterValue);
                } else {
                    method.addParameter(parameterName, parameterValue.toString());
                }
            }
        }
        if (requestEntity instanceof MultipartRequestEntity) {
            method.setRequestEntity(requestEntity);
        } else {
            if (isUseRequestBody && urlParams != null && !urlParams.isEmpty()) {
                String urlParamsStr = StringUtils.join(urlParams, "&");
                url = url + (url.indexOf("?") != -1 ? "&" : "?") + urlParamsStr;
                method = new PostMethod(url);
                method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
            }
            if (headers != null && !headers.isEmpty()) {
                Set<String> set = headers.keySet();
                for (String key : set) {
                    method.addRequestHeader(key, headers.get(key));
                }
            }
            if (requestEntity != null) {
                method.setRequestEntity(requestEntity);
            }
        }
        HttpClient hc = new HttpClient();
        int status = hc.executeMethod(method);
        if (status == HttpStatus.SC_OK) {
            if (returnType == null || (returnType != null && returnType == ReturnType.String)) {
                return method.getResponseBodyAsString();
            } else if (returnType == ReturnType.byteArray) {
                return method.getResponseBody();
            } else {
                return method.getResponseBodyAsStream();
            }
        } else {
            return null;
        }
    }

    /**
     * 模拟上传文件(远程URL)
     *
     * @param uploadUrl 上传路径
     * @param picUrl    远程图片URL
     * @param params    参数内容
     * @return
     * @throws IOException
     * @Title uploadFile
     * @author zh
     * @date 2016年6月30日 下午4:22:10
     */
    public static String uploadFile(String uploadUrl, URL picUrl, Map<String, String> params) throws IOException {
        return uploadFile(uploadUrl, picUrl.openStream(), picUrl.getFile().substring(picUrl.getFile().lastIndexOf("/")) + 1, params);
    }

    /**
     * 模拟上传文件(本地文件)
     *
     * @param uploadUrl 上传路径
     * @param file      本地文件
     * @param params    参数内容
     * @return
     * @Title uploadFile
     */
    public static String uploadFile(String uploadUrl, File file, Map<String, Object> params) throws IOException {
        List<File> files = new ArrayList<File>();
        files.add(file);
        return uploadFile(uploadUrl, files, params);
    }

    /**
     * 模拟上传文件，支持多文件上传(本地文件)
     *
     * @param uploadUrl 上传路径
     * @param files     本地文件列表
     * @param params    参数内容
     * @return
     * @Title uploadFile
     */
    public static String uploadFile(String uploadUrl, List<File> files, Map<String, Object> params) throws IOException {
        Map<String, File> filesMap = new HashMap<>();
        for (File file : files) {
            filesMap.put("file_" + SequenceUtil.getLowerCaseUUID(), file);
        }
        return uploadFile(uploadUrl, filesMap, params);
    }

    /**
     * 模拟上传文件，支持多文件上传(本地文件)
     *
     * @param uploadUrl 上传路径
     * @param files     本地文件列表
     * @param params    参数内容
     * @return
     * @throws IOException
     */
    public static String uploadFile(String uploadUrl, Map<String, File> files, Map<String, Object> params) throws IOException {
        PostMethod method = new PostMethod(uploadUrl);
        HttpMethodParams methodParams = method.getParams();
        methodParams.setContentCharset(StandardCharsets.UTF_8.toString());
        methodParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, StandardCharsets.UTF_8.toString());
        boolean includeFile = files != null && !files.isEmpty();
        boolean hasParams = params != null && !params.isEmpty();
        int length = (includeFile ? files.size() : 0) + (hasParams ? params.size() : 0);
        Part[] parts = new Part[length];
        int index = 0;// 参数下标
        if (hasParams) {
            // 设置出文件参数以外的参数
            Set<String> keys = params.keySet();
            for (String key : keys) {
                Object value = params.get(key);
                parts[index++] = new StringPart(key, value == null ? "" : value.toString(), StandardCharsets.UTF_8.toString());
            }
        }
        if (includeFile) {
            // 设置文件参数
            Set<String> keys = files.keySet();
            for (String key : keys) {
                FilePart filePart = new FilePart(key, files.get(key));
                parts[index++] = filePart;
            }
        }
        MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, methodParams);
        method.setRequestEntity(requestEntity);
        HttpClient hc = new HttpClient();
        hc.executeMethod(method);
        String result = method.getResponseBodyAsString();
        return result;
    }

    /**
     * 模拟上传文件（输入流）
     *
     * @param uploadUrl 上传链接
     * @param is        输入流
     * @param filename  文件名
     * @param params    参数
     * @return
     * @Title uploadFile
     */
    public static String uploadFile(String uploadUrl, InputStream is, String filename, Map<String, String> params) throws IOException {
        StringBuffer responseStr = new StringBuffer();
        DataInputStream fs = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        ByteArrayOutputStream baos = null;
        try {
            URL urlGet = new URL(uploadUrl);
            HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            conn.setRequestProperty("Charsert", "UTF-8");
            // 定义数据分隔线
            String BOUNDARY = "-------" + System.nanoTime() + "-------";
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            os = conn.getOutputStream();
            dos = new DataOutputStream(os);
            // 文件内容
            StringBuilder contentBody = new StringBuilder();
            String end = "\r\n";
            contentBody.append("--").append(BOUNDARY).append(end);
            contentBody.append("Content-Disposition: form-data;name=\"media\";filename=\"" + filename + "\"").append(end);
            contentBody.append("Content-Type:application/octet-stream").append(end).append(end);
            dos.write(contentBody.toString().getBytes());
            fs = new DataInputStream(is);
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = fs.read(bufferOut)) != -1) {
                dos.write(bufferOut, 0, bytes);
            }
            // 参数内容
            if (params != null && params.size() > 0) {
                for (String key : params.keySet()) {
                    StringBuilder paramData = new StringBuilder();
                    paramData.append(end).append("--").append(BOUNDARY).append(end);
                    paramData.append("Content-Disposition: form-data;name=\"" + key + "\";");
                    paramData.append(end);
                    paramData.append(end);
                    paramData.append("\"" + params.get(key) + "\"");
                    dos.write(paramData.toString().getBytes());
                }
            }
            // 最后一个片段结尾要用--表示
            byte[] end_data = (end + "--" + BOUNDARY + "--" + end).getBytes();
            dos.write(end_data);
            dos.flush();
            is = conn.getInputStream();
            baos = getRemoteResponse(is);
            responseStr.append(new String(baos.toByteArray(), ENCODING));
            if (conn != null) {
                conn.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(dos);
            IOUtils.closeQuietly(fs);
        }
        return responseStr.toString();
    }

    /**
     * 上传微信临时素材文件
     *
     * @param file 需要上传到的媒体文件
     * @param type 媒体文件类型，分别有图片（image）、语音（voice）、视频（video）、普通文件(file)、缩略图（thumb）
     * @return <pre>
     * {
     *    "type": "image",
     *    "media_id": "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
     *    "created_at": "1380000000"
     * }
     * </pre>
     */
    public static String uploadTemporaryMediaFile(String url, File file, String type) {
        try {
            return uploadFile(url, file, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下载远程文件
     *
     * @param httpUrl 远程该文件地址
     * @return
     */
    public static RemoteFile downWechatRemoteFile(String httpUrl) {
        log.info("开始下载远程文件：" + httpUrl);
        RemoteFile rf = new RemoteFile();
        File tempFile = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        String fileName = null;
        OutputStream out = null;
        try {
            URL url = new URL(httpUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            Map<String, List<String>> headerFields = conn.getHeaderFields();
            List<String> contentDisposition = headerFields.get("Content-disposition");
            if (contentDisposition != null && !contentDisposition.isEmpty()) {
                String contentDispositionValue = contentDisposition.get(0);
                fileName = StringUtils.substringBetween(contentDispositionValue, "attachment; filename=\"", "\"");
                String[] fileNameArr = StringUtils.split(fileName, ".");
                tempFile = File.createTempFile(fileNameArr[0], "." + fileNameArr[1]);
            }
            is = conn.getInputStream();
            baos = HttpUtil.getRemoteResponse(is);
            List<String> contentType = headerFields.get("Content-Type");
            if (contentType != null && !contentType.isEmpty()) {
                if (contentType.get(0).contains("application/json")) {
                    String responseStr = new String(baos.toByteArray(), ENCODING);
                    JSONObject json = JSONObject.fromObject(responseStr);
                    rf.setErrorCode(json.getString("errcode"));
                    rf.setErrorMsg(json.getString("errmsg"));
                    log.info(responseStr);
                } else {
                    out = new FileOutputStream(tempFile);
                    baos.writeTo(out);
                    rf.setFile(tempFile);
                }
            }
            if (conn != null) {
                conn.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(baos);
        }
        if (rf.getFile() != null) {
            log.info("下载成功!");
        } else {
            log.info("下载失败!");
        }
        return rf;
    }

    /**
     * 读取输入流中字节数据
     *
     * @param is 输入流
     * @return
     * @throws IOException
     */
    public static ByteArrayOutputStream getRemoteResponse(InputStream is) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] b = new byte[100];
        int len = 0;
        while ((len = is.read(b, 0, 100)) > 0) {
            swapStream.write(b, 0, len);
        }
        swapStream.close();
        return swapStream;
    }

    /**
     * 将map形式的参数转换成:参数名=参数值的形式
     *
     * @param params 参数映射
     * @return
     */
    private static String switchParamsModel(Map<String, Object> params) {
        List<String> paramsList = new ArrayList<String>();
        if (params != null && !params.isEmpty()) {
            Set<String> keys = params.keySet();
            for (String parameterName : keys) {
                Object parameterValue = params.get(parameterName);
                paramsList.add(parameterName + "=" + parameterValue);
            }
        }
        return StringUtils.join(paramsList, "&");
    }

    /**
     * 发起远程Http/https请求
     *
     * @param httpUrl   请求地址
     * @param useHttps  是否使用https请求方式
     * @param paramsStr 参数字符串 格式：paramNmae1=paramValue1 & paramName2=paramValue2...
     * @return
     * @throws Exception
     */
    private static String sendRemoteRequest(String httpUrl, boolean useHttps, RequestMethod method, String paramsStr) {
        StringBuffer responseStr = new StringBuffer();
        responseStr = new StringBuffer();
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            boolean includeParams = StringUtils.isNotBlank(paramsStr);
            if (includeParams && method == RequestMethod.GET) {
                if (httpUrl.contains("?")) {
                    httpUrl += "&" + paramsStr;
                } else {
                    httpUrl += "?" + paramsStr;
                }
            }
            URL url = new URL(httpUrl);
            URLConnection conn = null;
            HttpsURLConnection httpsConn = null;
            HttpURLConnection httpConn = null;
            if (useHttps) {// 使用https形式
                httpsConn = (HttpsURLConnection) url.openConnection();
                conn = httpsConn;
            } else {// 普通http形式
                httpConn = (HttpURLConnection) url.openConnection();
                conn = httpConn;
            }
            conn.setConnectTimeout(600 * 1000);
            boolean isPost = method == RequestMethod.POST;
            if (isPost) {
                conn.setDoInput(true);
            }
            conn.setDoOutput(true);
            if (includeParams && isPost) {
                conn.getOutputStream().write(paramsStr.getBytes());
            }
            is = conn.getInputStream();
            baos = getRemoteResponse(is);
            responseStr.append(new String(baos.toByteArray(), ENCODING));
            if (httpsConn != null && conn instanceof HttpsURLConnection) {
                httpsConn.disconnect();
            } else if (httpConn != null && conn instanceof HttpURLConnection) {
                httpConn.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(baos);
        }
        return responseStr.toString();
    }

    public static enum ReturnType {
        /**
         * 直接数组
         **/
        byteArray, /**
         * 字符串
         **/
        String, /**
         * 流
         **/
        Stream
    }

    public static enum RequestMethod {
        POST, GET
    }

    public static class MimeType {

        public final static String APPLICATION_JSON = "application/json";
        public final static String APPLICATION_OCTET_STREAM = "application/octet-stream";
        public final static String APPLICATION_XML = "application/xml";
        public final static String IMAGE_GIF = "image/gif";
        public final static String IMAGE_JPEG = "image/jpeg";
        public final static String IMAGE_PNG = "image/png";
        public final static String TEXT_HTML = "text/html";
        public final static String TEXT_PLAIN = "text/plain";
        public final static String TEXT_XML = "text/xml";

    }
}
